package drp.snowbell.part;

import java.util.Collection;
import java.util.Map;

import weibo.toyger.common.reflection.ReflectionHelper;
import weibo.toyger.common.util.LockableMap;
import weibo.toyger.core.beans.IEntity;
import weibo.toyger.core.farm.Property;


public class PartBlueprint {

	public PartBlueprint(Class<? extends IPart> skillClass, String name, Collection<Property> propertyList, String defaultParameter) {
		this.name = name;
		LockableMap<String, Property> pMap = new LockableMap<String, Property>();
		for (Property prop : propertyList) {
			pMap.put(prop.getName(), prop);
		}
		pMap.lock();
		propertyMap = pMap;
		this.defaultParameter = defaultParameter;
		this.skillClass = skillClass;
	}

	private final String defaultParameter;
	private final String name;
	private final Map<String, Property> propertyMap;
	private final Class<? extends IPart> skillClass;

	private IPart blankSkill;

	// 技能名称
	public String getName() {
		return name;
	}

	public Collection<Property> getPropertyList() {
		return propertyMap.values();
	}

	public Property getProperty(String propertyName) {
		Property ret = null;
		if (propertyMap.containsKey(propertyName)) {
			ret = propertyMap.get(propertyName);
		}
		return ret;
	}

	public Class<? extends IPart> getSkillClass() {
		return skillClass;
	}

	/*
	 * 获取属于哪个模块的名称.
	 * 
	 * @return 模块名.
	 * 
	 * public String getBeanName() { return beanName; }
	 */

	/**
	 * 新建Skill实例.
	 * 
	 * @param beanEntry
	 *            Skill所属的TBean
	 * @return Skill实例.
	 */
	public IPart newInstance(IEntity beanEntry) {
		if (beanEntry == null) {
			throw new IllegalArgumentException("beanEntry");
		}
		if (blankSkill == null) {
			blankSkill = ReflectionHelper.newInstance(getSkillClass());
		}
		IPart ret = blankSkill.cloneSkill();
		ret.setTbean(beanEntry);
		ret.setParameter(defaultParameter);
		return ret;
	}

	public String getDefaultParameter() {
		return defaultParameter;
	}
}
